]> dgit.raspbian.org Git - gtk+3.0.git/commitdiff
clipboard: improve EOL conversion of plain text
authorIgnazio Pillai <ignazp@amazon.com>
Thu, 22 Dec 2022 09:20:20 +0000 (10:20 +0100)
committerIgnazio Pillai <ignazp@amazon.com>
Thu, 22 Dec 2022 09:26:34 +0000 (10:26 +0100)
Handle the case of clipboard text with CR line endings

gdk/win32/gdkselection-win32.c

index a047a4ada5d60ce9487f22f2f13774f85fffb601..caa1a86a6de5a2f7984d4fc212a5960e70c8ea96 100644 (file)
@@ -1068,23 +1068,30 @@ transmute_cf_unicodetext_to_utf8_string (const guchar    *data,
                                          gint            *set_data_length,
                                          GDestroyNotify  *set_data_destroy)
 {
-  wchar_t *ptr, *p, *q;
+  wchar_t *ptr, *p, *q, *endp;
   gchar *result;
   glong wclen, u8_len;
 
-  /* Strip out \r */
+  /* Replace CR and CR+LF with LF */
   ptr = (wchar_t *) data;
   p = ptr;
   q = ptr;
+  endp = ptr + length / 2;
   wclen = 0;
 
-  while (p < ptr + length / 2)
+  while (p < endp)
     {
       if (*p != L'\r')
         {
           *q++ = *p;
           wclen++;
         }
+      else if (p + 1 >= endp || *(p + 1) != L'\n')
+        {
+          *q++ = L'\n';
+          wclen++;
+        }
+
       p++;
     }
 
@@ -1269,24 +1276,31 @@ transmute_cf_text_to_utf8_string (const guchar    *data,
                                   gint            *set_data_length,
                                   GDestroyNotify  *set_data_destroy)
 {
-  char *ptr, *p, *q;
+  char *ptr, *p, *q, *endp;
   gchar *result;
   glong wclen, u8_len;
   wchar_t *wstr;
 
-  /* Strip out \r */
+  /* Replace CR and CR+LF with LF */
   ptr = (gchar *) data;
   p = ptr;
   q = ptr;
+  endp = ptr + length / 2;
   wclen = 0;
 
-  while (p < ptr + length / 2)
+  while (p < endp)
     {
       if (*p != '\r')
         {
           *q++ = *p;
           wclen++;
         }
+      else if (p + 1 > endp || *(p + 1) != '\n')
+        {
+          *q++ = '\n';
+          wclen++;
+        }
+
       p++;
     }